﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Finance.TodoSoft;

namespace Finance.GUI
{
	public partial class FormOkCancel : Form
	{
		public FormOkCancel()
		{
			InitializeComponent();
		}

		DialogResult _result = DialogResult.Cancel;

		public event EventHandler<ApplyEventArgs> OnApply;

		protected bool Apply()
		{
			//Hack for forcing update from view to viewmodel binding.
			foreach (Control control in Controls)
			{
				control.Focus();	
			}
			
			var args = new ApplyEventArgs();

			OnApply.InvokeIfNotNull(this, args);

			return args.IsDataValid;
		}

		private void OnMenuItemOkClick(object sender, EventArgs e)
		{
			if (Apply())
			{
				_result = DialogResult.OK;
				Close();
			}
		}

		private void OnMenuItemCancelClick(object sender, EventArgs e)
		{
			_result = DialogResult.Cancel;
			Close();
		}

		private void OnFormClosing(object sender, CancelEventArgs e)
		{
			DialogResult = _result;
		}
	}

	public class ApplyEventArgs : EventArgs
	{
		public bool IsDataValid = true;
	}

}